﻿using System;

namespace YaAppoint.LocalCache
{
    /// <summary>
    /// 缓存项目
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CacheItem<T>
    {
        public T Value { get; set; }

        /// <summary>
        ///  创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 设置过期时间
        /// </summary>
        public TimeSpan? Expiration { get; set; }

        public DateTime AbsoluteExpiration => Expiration.HasValue ? CreatedTime.Add(Expiration.Value) : DateTime.MaxValue;

        public bool IsExpired => DateTime.Now > AbsoluteExpiration;

        public CacheItem(T value,TimeSpan? expiration=null) 
        {
            Value = value;
            CreatedTime = DateTime.Now;
            Expiration = expiration;
        }
    }
}
